package bravo.xc.com.xc_bravo_android.operation.bankcard.addbank;

import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import bravo.xc.com.xc_bravo_android.R;
import bravo.xc.com.xc_bravo_android.adapter.spinnerAdapter;
import bravo.xc.com.xc_bravo_android.base.BaseMVPActivity;
import bravo.xc.com.xc_bravo_android.beans.AddBeans;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class AddBankcardActivity extends BaseMVPActivity<AddBankcardContract.AddBankView,AddBankcardContract.AddBankPresenter> implements AddBankcardContract.AddBankView, View.OnClickListener{
    private LinearLayout layout_title_left;
    private TextView tv_title_meg;
    private TextView openCard;
    private Spinner addBankcardSpinner;
    private EditText card_people;//持卡人
    private EditText cardNum;//卡号
    private Button confirm;//确认
    private spinnerAdapter spinnerAdapter;
    private AddBeans addBeans;
    private List<String> adapter;
    private int selectedItem ;

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.layout_title_left:
                finish();
                break;
            case R.id.btn_bankcard_confirm:
                if(checkNull()) {

                    addBeans.setBankAddress(openCard.getText().toString().trim());
                    addBeans.setRealname(card_people.getText().toString().trim());
                    addBeans.setUserId(String.valueOf(getSharedPreferences("UserInfo",MODE_PRIVATE).getInt("userId",0)));
                    addBeans.setBankName(adapter.get(selectedItem));
                    addBeans.setBankNum(cardNum.getText().toString().trim());
                    addBeans.setDefaultX("F");
                    presenter.addBank(addBeans);
                }
                break;
        }
    }

    @Override
    public void showToast(String str) {
        Toast.makeText(AddBankcardActivity.this,str,Toast.LENGTH_SHORT).show();
    }

    @Override
    protected AddBankcardContract.AddBankPresenter createPresnter() {
        return new AddBankcardPresenter();
    }

    @Override
    public void setAdapter(List<String> adapter) {

        this.adapter = adapter;
        spinnerAdapter = new spinnerAdapter(this,R.layout.item_spinner,adapter);
        spinnerAdapter.setDropDownViewResource(R.layout.item_spinner);//设置下拉样式
        addBankcardSpinner.setAdapter(spinnerAdapter);
        addBankcardSpinner.setSelection(0);
        //addBankcardSpinner.setSelection(adapter.size() - 1, true);//显示List最后一项作为Hint
    }

    @Override
    public void setNull() {
        card_people.setText("");
        cardNum.setText("");
        openCard.setText("");
    }

    @Override
    protected void initView() {
        openCard = findViewById(R.id.et_bankcard_card_open);
        layout_title_left=(LinearLayout)findViewById(R.id.layout_title_left);
        layout_title_left.setOnClickListener(this);
        tv_title_meg=(TextView)findViewById(R.id.tv_title_msg);
        tv_title_meg.setText("添加银行卡");
        card_people=(EditText)findViewById(R.id.et_bankcard_card_people);
        cardNum=(EditText)findViewById(R.id.et_bankcard_card_num);
        confirm=(Button)findViewById(R.id.btn_bankcard_confirm);
        confirm.setOnClickListener(this);
        addBankcardSpinner=(Spinner)findViewById(R.id.sp_bankcard_add_bankcard);



        addBankcardSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                selectedItem = position;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                Toast.makeText(AddBankcardActivity.this,"你什么都未选中",Toast.LENGTH_SHORT).show();
            }

        });
    }

    @Override
    protected void initData() {
        presenter.getSupportBank();
        selectedItem = -1;
        addBeans = new AddBeans();
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_add_bankcard;
    }

    @Override
    protected Unbinder getBinder() {
        return ButterKnife.bind(this);
    }

    @Override
    protected void onChildDestroy() {
        presenter.onDetached();
    }

    public boolean checkNull() {
        if (card_people.getText().toString().trim().isEmpty()) {
            showToast("请填写持卡人");
            return false;
        }
        if (cardNum.getText().toString().trim().isEmpty()) {
            showToast("请填写卡号");
            return false;
        }
        if (openCard.getText().toString().trim().isEmpty()) {
            showToast("请填写开户行");
            return false;
        }
        return true;
    }



}
